home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / System7 tools / M / Moniker 2.0.1 ƒ / Source ƒ / Moniker 2.0.1 Misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-20  |  6.5 KB  |  269 lines  |  [TEXT/KAHL]

  1. /* Moniker 2.0.1 */
  2. /* Copyright ©1992 by Michael J. Simms */
  3. /* Modified 12/20/92 by Sean Hummel */
  4.  
  5.  
  6. /* Include the program header. */
  7. #include "Moniker 2.0.1.h"
  8. #include "AppleEvents.h"
  9. #include "Aliases.h"
  10. #include "GestaltEqu.h"
  11. #include "Traps.h"
  12. #include "Memory.h"
  13. #include "string.h"
  14.  
  15. /* Function prototypes. */
  16. OSErr makeAlias(FSSpec);
  17. void initialize(void);
  18. Boolean canRunProgram(void);
  19. void installAEHandlers(void);
  20. void removeAEHandlers(void);
  21. void showExplainBox(void);
  22. short NumToolboxTraps(void);
  23. TrapType GetTrapType(short);
  24. Boolean TrapAvailable(short);
  25. void describeError(short);
  26.  
  27. /* Declarations. */
  28. extern    pascal OSErr HandleODOC(AppleEvent *, AppleEvent *, long);
  29. extern    pascal OSErr HandleQUIT(AppleEvent *, AppleEvent *, long);
  30. extern    pascal OSErr HandleOAPP(AppleEvent *, AppleEvent *, long);
  31. extern    pascal OSErr HandlePDOC(AppleEvent *, AppleEvent *, long);
  32. extern    Boolean    gQuitDemand;
  33.  
  34.  
  35. OSErr
  36. makeAlias(FSSpec fileFSSpec)
  37. /* Create an alias. */
  38. {
  39.     short                aliasRefNum,myResFile;
  40.     OSErr                myErr;
  41.     Point                where={84,50};
  42.     FInfo                aliasFInfo,theFileFInfo;
  43.     Str255                saveString;
  44.     FSSpec                aliasFSSpec;
  45.     AliasHandle            aliasHandle;
  46.     StandardFileReply    reply;
  47.     EventRecord            event;
  48.     Str255                volName;
  49.  
  50.     /* Call Standard File Package. */
  51.     GetIndString(saveString,kSaveSTRs,kSaveStr);
  52.     
  53.     if (!gNameManually) {
  54.                 CustomPutFile(saveString,fileFSSpec.name,&reply,kPutFile,where,NIL,NIL,NIL,NIL,NIL);
  55.         }/*if*/
  56.     else { HGetVol(volName,&reply.sfFile.vRefNum,&reply.sfFile.parID);
  57.            reply.sfGood = TRUE;
  58.            BlockMove(&fileFSSpec.name,&reply.sfFile.name,sizeof(reply.sfFile.name));
  59.            }/*else*/
  60.     
  61.     
  62.     
  63.  
  64.     /* If the user gave the go-ahead then save the file. */
  65.     if (reply.sfGood) {
  66.  
  67.         /* Fill the 'aliasFSSpec' structure. */
  68.         FSMakeFSSpec(reply.sfFile.vRefNum,reply.sfFile.parID,
  69.             reply.sfFile.name,&aliasFSSpec);
  70.  
  71.         /* Create the alias record. */
  72.         myErr=NewAlias(NIL,&fileFSSpec,&aliasHandle);
  73.  
  74.         /* Save the current resource file. */
  75.         myResFile=CurResFile();
  76.  
  77.         /* Get info on the file we are making an alias of. */
  78.         FSpGetFInfo(&fileFSSpec,&theFileFInfo);
  79.  
  80.         /* Create and open the file. */
  81.         FSpCreateResFile(&aliasFSSpec,theFileFInfo.fdCreator,theFileFInfo.fdType,reply.sfScript);
  82.         aliasRefNum=FSpOpenResFile(&aliasFSSpec,fsCurPerm);
  83.  
  84.         /* If possible, write to the file. */
  85.         if (aliasRefNum!=kCantOpen) {
  86.  
  87.             /* Write the resource. */
  88.             UseResFile(aliasRefNum);
  89.             AddResource((Handle)aliasHandle,rAliasType,kAliasRsrcID,aliasFSSpec.name);
  90.             WriteResource((Handle)aliasHandle);
  91.             CloseResFile(aliasRefNum);
  92.  
  93.             /* Set the finder flags. */
  94.             theFileFInfo.fdFlags=kAliasFlag;
  95.             FSpSetFInfo(&aliasFSSpec,&theFileFInfo);
  96.  
  97.         } else describeError(kCantOpen);
  98.  
  99.         /* Set resource file back to this application's. */
  100.         UseResFile(myResFile);
  101.     }
  102. }
  103.  
  104.  
  105. void
  106. initialize(void)
  107. /* Initialization routine. */
  108. {
  109.     MaxApplZone();
  110.     FlushEvents(everyEvent,0);
  111.  
  112.     /* Initialize stuff. */
  113.     InitGraf(&thePort);
  114.     InitFonts();
  115.     InitWindows();
  116.     InitMenus();
  117.     TEInit();
  118.     InitDialogs(NIL);
  119.     InitCursor();
  120.  
  121.     /* Draw the menu bar. */
  122.     DrawMenuBar();
  123. }
  124.  
  125.  
  126. Boolean
  127. canRunProgram(void)
  128. /* Returns TRUE if the program can run in the current environment. */
  129. {
  130.     long    myErr,response;
  131.     Boolean    haveGestalt,haveAliasMgr=FALSE,haveAppleEvents=FALSE,canRun;
  132.  
  133.     /* Get information on the current system setup. */
  134.     if (haveGestalt=TrapAvailable(_GestaltDispatch)) {
  135.  
  136.         if (Gestalt(gestaltAliasMgrAttr,&response)==noErr) {
  137.             /* The default for 'haveAliasMgr' is FALSE. */
  138.             if (BitTst(&response,31-gestaltAliasMgrPresent)) haveAliasMgr=TRUE;
  139.         }
  140.  
  141.         if (Gestalt(gestaltAppleEventsAttr,&response)==noErr) {
  142.             /* The default for 'haveAppleEvents' is FALSE. */
  143.             if (BitTst(&response,31-gestaltAppleEventsPresent)) haveAppleEvents=TRUE;
  144.         }
  145.  
  146.         canRun=haveAliasMgr&&haveAppleEvents;
  147.     }
  148.  
  149.     /* Return TRUE if we can run this program. */
  150.     return(canRun);
  151. }
  152.  
  153.  
  154. void
  155. installAEHandlers(void)
  156. /* Installs Apple Event handlers. */
  157. {
  158.     AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,
  159.         (EventHandlerProcPtr)HandleODOC,0,FALSE);
  160.     AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,
  161.         (EventHandlerProcPtr)HandleQUIT,0,FALSE);
  162.     AEInstallEventHandler(kCoreEventClass,kAEPrintDocuments,
  163.         (EventHandlerProcPtr)HandlePDOC,0,FALSE);
  164.     AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,
  165.         (EventHandlerProcPtr)HandleOAPP,0,FALSE);
  166. }
  167.  
  168.  
  169. void
  170. removeAEHandlers(void)
  171. /* Removes Apple Event Handlers. */
  172. {
  173.     AERemoveEventHandler(kCoreEventClass,kAEOpenDocuments,
  174.         (EventHandlerProcPtr)HandleODOC,FALSE);
  175.     AERemoveEventHandler(kCoreEventClass,kAEQuitApplication,
  176.         (EventHandlerProcPtr)HandleQUIT,FALSE);
  177.     AERemoveEventHandler(kCoreEventClass,kAEPrintDocuments,
  178.         (EventHandlerProcPtr)HandlePDOC,FALSE);
  179.     AERemoveEventHandler(kCoreEventClass,kAEOpenApplication,
  180.         (EventHandlerProcPtr)HandleOAPP,FALSE);
  181. }
  182.  
  183.  
  184. void
  185. showExplainBox(void)
  186. /* Shows the dialog explaining the purpose of this program. */
  187. {
  188.     Boolean        gotEvent;
  189.     DialogPtr    explainDLOG;
  190.     EventRecord    event;
  191.  
  192.     /* Create the 'explain' dialog. */
  193.     explainDLOG=GetNewDialog(kExplainDLOGid,NIL,(DialogPtr)-1);
  194.     /* Show the 'explain' dialog. */
  195.     DrawDialog(explainDLOG);
  196.     do {
  197.     } while (!(gotEvent=WaitNextEvent(mDownMask+keyDownMask,&event,10L,NIL)));
  198.     /* Dispose of the 'explain' dialog. */
  199.     DisposDialog(explainDLOG);
  200. }
  201.  
  202.  
  203. short
  204. NumToolboxTraps(void)
  205. /* Returns the number of Toolbox traps. */
  206. {
  207.     short    result;
  208.  
  209.     if (NGetTrapAddress(_InitGraf,ToolTrap)==NGetTrapAddress(0xAA6E,ToolTrap))
  210.         result=0x200;
  211.     else result=0x400;
  212.     return(result);
  213. }
  214.  
  215.  
  216. TrapType
  217. GetTrapType(short theTrap)
  218. /* Returns the type of 'theTrap'. */
  219. {
  220.     TrapType    result;
  221.  
  222.     if ((theTrap&kTrapMask)>0) result=ToolTrap;
  223.     else result=OSTrap;
  224.     return(result);
  225. }
  226.  
  227.  
  228. Boolean
  229. TrapAvailable(short theTrap)
  230. /* Returns TRUE if 'theTrap' is available. */
  231. {
  232.     Boolean        result;
  233.     TrapType    theTrapType;
  234.  
  235.     theTrapType=GetTrapType(theTrap);
  236.     if (theTrapType==ToolTrap) {
  237.         theTrap=(theTrap&0x07FF);
  238.         if (theTrap>=NumToolboxTraps()) theTrap=_Unimplemented;
  239.     }
  240.     result=(NGetTrapAddress(theTrap,theTrapType)!=NGetTrapAddress(_Unimplemented,ToolTrap));
  241.     return(result);
  242. }
  243.  
  244.  
  245. void
  246. describeError(short id)
  247. /* Presents the user with an alert box describing the error that was */
  248. /* (hopefully) not my fault. */
  249. {
  250.     Str255 str;
  251.  
  252.     switch(id) {
  253.         /* Error handling for this program. */
  254.         case kCantRun    : GetIndString(str,kErrorSTRs,kNoAliasMgrStr);
  255.             break;
  256.         case kCantOpen    : GetIndString(str,kErrorSTRs,kCantOpenStr);
  257.             break;
  258.         /* Default message. */
  259.         default            : GetIndString(str,kErrorSTRs,kDefaultStr);
  260.             break;
  261.     }
  262.  
  263.     /* Display the error message. */
  264.     ParamText(str,"\p","\p","\p");
  265.     Alert(kErrorALRTid,NIL);
  266.  
  267.     /* Set 'gQuitDemand' to TRUE. */
  268.     gQuitDemand=TRUE;
  269. }